﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using Autorent.Desktop.Model;
using Autorent.Desktop.Model.AutorentMngrServiceReference;

namespace Autorent.Desktop.Modules.Employees.Services
{
    public class EmployeesRepository :AutorentEntitiesContext, IEmployeesRepository
    {       

        public ObservableCollection<Employee> GetEmployees()
        {
            try
            {
                return new ObservableCollection<Employee>(Context.Employees.Expand("EmployeeAddresses"));
            }
            catch (Exception ex)
            {
                throw new AutorentModelException("Error occured while connecting service", ex.InnerException);
            }
        }

        public void AddEmployee(Employee employee)
        {
            Context.AddToEmployees(employee);
            try
            {
                Context.SaveChanges();
            }
            catch (Exception ex)
            {
                throw new AutorentModelException("Error occured while connecting service", ex.InnerException);
            }
        }

        public void UpdateEmployee(Employee employee)
        {
            try
            {
                Context.UpdateObject(Context.Employees.Where<Employee>(e => e.Employee_ID.Equals(employee.Employee_ID)).First());
                Context.SaveChanges();
            }
            catch (Exception ex)
            {
                throw new AutorentModelException("Error occured while updating object", ex.InnerException);
            }
        }

        public void DeleteEmployee(Employee employee)
        {
            try
            {
                Context.DeleteObject(Context.Employees.Where<Employee>(e => e.Employee_ID.Equals(employee.Employee_ID)).First());
                Context.SaveChanges();
            }
            catch (Exception ex)
            {
                throw new AutorentModelException("Error occured while deleting object", ex.InnerException);
            }
        }

        public void AddSystemUser(SystemUser systemUser)
        {
            try
            {
                Context.AddToSystemUsers(systemUser);
                Context.SaveChanges();
            }
            catch (Exception ex)
            {
                throw new AutorentModelException("Error occured while adding system user", ex.InnerException);
            }
        }

        public void UpdateSystemUser(SystemUser systemUser)
        {
            try
            {
                Context.AddToSystemUsers(Context.SystemUsers.Where<SystemUser>(u => u.SystemUser_ID.Equals(systemUser.SystemUser_ID)).First());
                Context.SaveChanges();
            }
            catch (Exception ex)
            {
                throw new AutorentModelException("Error occured while updating object", ex.InnerException);
            }
        }

        public void DeleteSystemUser(SystemUser systemUser)
        {
            try
            {
                Context.DeleteObject(Context.SystemUsers.Where<SystemUser>(u => u.SystemUser_ID.Equals(systemUser.SystemUser_ID)).First());
                Context.SaveChanges();
            }
            catch (Exception ex)
            {
                throw new AutorentModelException("Error occured while deleting object", ex.InnerException);
            }
        }
    }
}
